package org.example.springbootwebexample.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.springbootwebexample.pojo.Emp;
import org.example.springbootwebexample.pojo.Result;
import org.example.springbootwebexample.service.EmpService;
import org.example.springbootwebexample.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping
    public Result login(@RequestBody Emp emp){

        log.info("员工{} 来登陆了", emp);
        Emp empLogin =  empService.login(emp);

        if(empLogin != null) {

            //1.生成JET令牌
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误~");
    }


}
